// 下载图片
import { OS } from "@/config/constants";
import { OSCheck } from "@/cordova/common";

// pc可以用此方法下载，ichangan内无效
export const downLoadImgToBase62 = (link: string, picName: string) => {
  const img = new Image();
  img.setAttribute("crossOrigin", "anonymous");
  img.onload = function () {
    const canvas = document.createElement("canvas");
    const context = canvas.getContext("2d");
    canvas.width = img.width;
    canvas.height = img.height;
    context.drawImage(img, 0, 0, img.width, img.height);
    const url = canvas.toDataURL("images/png");
    const a = document.createElement("a");
    const event = new MouseEvent("click");
    a.download = picName || "default.png";
    a.href = url;
    a.dispatchEvent(event);
  };
  img.src = link + "?v=" + Date.now();
};

// 图片转base64
export const image2Base64 = (img: any) => {
  const canvas = document.createElement("canvas");
  canvas.width = img.width;
  canvas.height = img.height;
  const ctx = canvas.getContext("2d");
  ctx.drawImage(img, 0, 0, img.width, img.height);
  const dataURL = canvas.toDataURL("image/png");
  return dataURL;
};

// base64转blob
export const base64ToBlob = (code) => {
  const parts = code.split(";base64,");
  const contentType = parts[0].split(":")[1];
  const raw = window.atob(parts[1]);
  const rawLength = raw.length;
  const uInt8Array = new Uint8Array(rawLength);
  for (let i = 0; i < rawLength; ++i) {
    uInt8Array[i] = raw.charCodeAt(i);
  }
  return new Blob([uInt8Array], {
    type: contentType,
  });
};
// 下载图片
export const loadCode = (img, name) => {
  // const image = new Image();
  // // 解决跨域 Canvas 污染问题
  // image.setAttribute("crossOrigin", "anonymous");
  // // console.log('imgsrc--', image);
  // image.src = imgsrc;
  // const that = this;
  img.onload = function () {
    const imgBase64 = image2Base64(img);
    // const canvas = document.createElement("canvas");
    // canvas.width = image.width;
    // canvas.height = image.height;
    // const context = canvas.getContext("2d");
    // context.drawImage(image, 0, 0, image.width, image.height);
    // const url = canvas.toDataURL("image/png"); // 得到图片的base64编码数据
    const a = document.createElement("a");
    const event = new MouseEvent("click");
    a.download = name || "image-" + new Date().getTime();
    // 区别 iso 和 android 下载方式
    if (OSCheck() === OS.Android) {
      const blob = base64ToBlob(imgBase64);
      a.href = URL.createObjectURL(blob);
    } else {
      a.href = imgBase64;
    }
    // if (that.curSystem === "android") {
    //   let blob = base64ToBlob(url);
    //   a.href = URL.createObjectURL(blob);
    // } else {
    //   a.href = url;
    // }
    a.dispatchEvent(event); // 触发a的单击事件
  };
};

// download img by base64
export const downloadImgbyBase64 = (base64: string, name: string) => {
  const a = document.createElement("a");
  const event = new MouseEvent("click");
  a.download = name || "image-" + new Date().getTime();
  // 区别 iso 和 android 下载方式
  if (OSCheck() === OS.Android) {
    const blob = base64ToBlob(base64);
    a.href = URL.createObjectURL(blob);
  } else {
    a.href = base64;
  }
  // if (that.curSystem === "android") {
  //   let blob = base64ToBlob(url);
  //   a.href = URL.createObjectURL(blob);
  // } else {
  //   a.href = url;
  // }
  a.dispatchEvent(event); // 触发a的单击事件
};
